package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsNoTxnRegMapper;
import cc.rengu.igas.bsps.common.entity.NoTxnReg;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/25
 */
public class BspsNoTxnRegMapperImpl implements BspsNoTxnRegMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    @Override
    public List<NoTxnReg> selectBspsNoTxnRegByManagerId(String instId, String managerId, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsNoTxnRegByManagerId");
        NoTxnReg noTxnReg = new NoTxnReg();
        noTxnReg.setInstId(instId);
        noTxnReg.setManagerId(managerId);
        noTxnReg.setBatchDate(startDate);
        noTxnReg.setRemark1(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, noTxnReg, NoTxnReg.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取无交易商户终端信息失败,instId:<{}>,managerId:<{}>,startDate:<{}>,endDate:<{}>", instId, managerId, startDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (NoTxnReg) item).collect(Collectors.toList());
    }

    @Override
    public List<NoTxnReg> selectBspsNoTxnRegByGroupMchntNo(String instId, String groupMchntNo, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsNoTxnRegByGroupMchntNo");
        NoTxnReg noTxnReg = new NoTxnReg();
        noTxnReg.setInstId(instId);
        noTxnReg.setGroupMchntNo(groupMchntNo);
        noTxnReg.setBatchDate(startDate);
        noTxnReg.setRemark1(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, noTxnReg, NoTxnReg.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取无交易商户终端信息失败,instId:<{}>,groupMchntNo:<{}>,startDate:<{}>,endDate:<{}>", instId, groupMchntNo, startDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (NoTxnReg) item).collect(Collectors.toList());
    }

    @Override
    public List<NoTxnReg> selectBspsNoTxnRegByPlatMchntNo(String instId, String platMchntNo, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsNoTxnRegByPlatMchntNo");
        NoTxnReg noTxnReg = new NoTxnReg();
        noTxnReg.setInstId(instId);
        noTxnReg.setPlatMchntNo(platMchntNo);
        noTxnReg.setBatchDate(startDate);
        noTxnReg.setRemark1(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, noTxnReg, NoTxnReg.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取无交易商户终端信息失败,instId:<{}>,platMchntNo:<{}>,startDate:<{}>,endDate:<{}>", instId, platMchntNo, startDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (NoTxnReg) item).collect(Collectors.toList());
    }

    @Override
    public List<NoTxnReg> selectBspsNoTxnRegByAgentMchntNo(String instId, String agentMchntNo, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsNoTxnRegByAgentMchntNo");
        NoTxnReg noTxnReg = new NoTxnReg();
        noTxnReg.setInstId(instId);
        noTxnReg.setAgentMchntNo(agentMchntNo);
        noTxnReg.setBatchDate(startDate);
        noTxnReg.setRemark1(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, noTxnReg, NoTxnReg.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取无交易商户终端信息失败,instId:<{}>,agentMchntNo:<{}>,startDate:<{}>,endDate:<{}>", instId, agentMchntNo, startDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (NoTxnReg) item).collect(Collectors.toList());
    }


    @Override
    public PageResult selectBspsNoTxnRegByGroupMchntNoPage(String instId, String groupMchntNo, String startDate, String endDate, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsNoTxnRegByGroupMchntNo");
        NoTxnReg noTxnReg = new NoTxnReg();
        noTxnReg.setInstId(instId);
        noTxnReg.setGroupMchntNo(groupMchntNo);
        noTxnReg.setBatchDate(startDate);
        noTxnReg.setRemark1(endDate);
        return dbsUtil.dbsFetchPageListData(sqlStr, noTxnReg, NoTxnReg.class, pageInfo);
    }

    @Override
    public PageResult selectBspsNoTxnRegByPlatMchntNoPage(String instId, String platMchntNo, String startDate, String endDate, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsNoTxnRegByPlatMchntNo");
        NoTxnReg noTxnReg = new NoTxnReg();
        noTxnReg.setInstId(instId);
        noTxnReg.setPlatMchntNo(platMchntNo);
        noTxnReg.setBatchDate(startDate);
        noTxnReg.setRemark1(endDate);
        return dbsUtil.dbsFetchPageListData(sqlStr, noTxnReg, NoTxnReg.class, pageInfo);
    }

    @Override
    public PageResult selectBspsNoTxnRegByAgentMchntNoPage(String instId, String agentMchntNo, String startDate, String endDate, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsNoTxnRegByAgentMchntNo");
        NoTxnReg noTxnReg = new NoTxnReg();
        noTxnReg.setInstId(instId);
        noTxnReg.setAgentMchntNo(agentMchntNo);
        noTxnReg.setBatchDate(startDate);
        noTxnReg.setRemark1(endDate);
        return dbsUtil.dbsFetchPageListData(sqlStr, noTxnReg, NoTxnReg.class, pageInfo);
    }

    @Override
    public PageResult selectBspsNoTxnRegByManagerIdPage(String instId, String managerId, String startDate, String endDate, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsNoTxnRegByManagerId");
        NoTxnReg noTxnReg = new NoTxnReg();
        noTxnReg.setInstId(instId);
        noTxnReg.setManagerId(managerId);
        noTxnReg.setBatchDate(startDate);
        noTxnReg.setRemark1(endDate);
        return dbsUtil.dbsFetchPageListData(sqlStr, noTxnReg, NoTxnReg.class, pageInfo);
    }

}
